(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//获取列表
	function getlist(curpage,pagenum){
	   $.ajax({
			url:'http://web.icecn.net/base/user/userList.json',
			type:'POST',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.result;
					if(dataset && dataset.length > 0){
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage==totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					};
				}
			}
	   });
	}
	//获取角色列表
	function getlist2(curpage,pagenum){
	   $.ajax({
			url:'http://web.icecn.net/base/role/roleList.json',
			type:'POST',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
			},
			dataType: "json",
			xhrFields:{withCredentials:true},
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.result;
					if(dataset && dataset.length > 0){
					   var html1 = template('selectlist',{dataset:dataset});
					   $("#roles").html(html1);
					   var html2 = template('datalist2',{dataset:dataset});
					   $("#example2").html(html2);
					};
				}
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//删除按钮
			$(".content-wrapper").on("click", '.delete', function() {
				$("#modal-delete .modal-body").attr("data-id",$(this).attr("data-id"))
				$("#modal-delete .modal-body").attr("data-userName",$(this).attr("data-userName"))
			})
			//确认删除
			$(".btndelete").click(function(){
			   var id=$("#modal-delete .modal-body").attr("data-id");
			   var userName=$("#modal-delete .modal-body").attr("data-userName");
			   $.ajax({
					url:'http://web.icecn.net/base/user/delete.json',
					type:'POST',
					async:true,
					data:{
						id:id,
						userName:userName
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-delete").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			//删除角色按钮
			$(".content-wrapper").on("click", '.deleterole', function() {
				$("#modal-deleterole .modal-body").attr("data-id",$(this).attr("data-id"))
			})
			//确认删除角色按钮
			$(".btndeleterole").click(function(){
			   var id=$("#modal-deleterole .modal-body").attr("data-id");
			   $.ajax({
					url:'http://web.icecn.net/base/role/del.json',
					type:'POST',
					async:true,
					data:{
						roleId:id
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-deleterole").modal('hide');
							getlist2(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			//新建后台账号
			$(".content-wrapper").on("click", '.addupdate', function() {
				$("#accountName").val("");
				$("#userName").val("");
				$("#email").val("");
				$("#roles option:first-child").prop("selected", 'selected');
			})
			//确认新建后台账号
			$(".btnaddupdate").click(
			  function(){
				 var accountName=$("#accountName").val()
				 var userName=$("#userName").val()
				 var email=$("#email").val()
				 var roles=$("#roles").val()
				 if(accountName==""){alert("请输入账号");return}
				   $.ajax({
						url:'http://web.icecn.net/base/user/save.json',
						type:'POST',
						async:true,
						data:{
							accountName:accountName,
							userName:userName,
							email:email,
							roles:roles
						},
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
								$("#modal-addupdate").modal('hide');
								getlist(curpage,pagenum)
							}else{
							   alert(data.msg)	
							}
						}
				   });
			  }
			)
			//新建角色
			$(".btnaddrole").click(
			  function(){
				 var name=$("#name").val()
				 if(name==""){alert("请输入角色名称");return}
				   $.ajax({
						url:'http://web.icecn.net/base/role/add.json',
						type:'POST',
						async:true,
						data:{
							name:name
						},
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
								$("#modal-addrole").modal('hide');
								getlist2(curpage,pagenum)
							}else{
							   alert(data.msg)	
							}
						}
				   });
			  }
			)
			//重置密码
			$(".content-wrapper").on("click", '.reset', function() {
				$("#modal-reset .modal-body").attr("data-id",$(this).attr("data-id"))
			})
			//确认重置密码
			$(".btnreset").click(function(){
				var userId=$("#modal-reset .modal-body").attr("data-id")
				   $.ajax({
						url:'http://web.icecn.net/base/user/rePassword.json',
						type:'POST',
						async:true,
						data:{
							userId:userId
						},
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
							   alert(data.msg)	
								$("#modal-reset").modal('hide');
								getlist(curpage,pagenum)
							}else{
							   alert(data.msg)	
							}
						}
				   });
		    })
			
			//配置用户权限
			$(".content-wrapper").on("click", '.Permissions', function() {
				$("#modal-Permissions .modal-body").attr("data-rid","")
				$("#modal-Permissions .modal-body").attr("data-uid","")
				var uid=$(this).attr("data-uid")
				var rid=$(this).attr("data-rid")
				if(uid==null){
				  $("#modal-Permissions .modal-body").attr("data-rid",rid)
				  $.ajax({
						url:'http://web.icecn.net/base/role/editRoleResources.json',
						type:'POST',
						async:true,
						data:{
							roleId:rid
						},
						dataType: "json",
					    xhrFields:{withCredentials:true},
						success: function(data){
							if(data.code === 200 ){
							  var a="";
							  var d=data.t;
							  if(d.length>0){
								  for(var i=0;i<d.length;i++){
									  if(d[i].children.length>0){
										  var b="";
										  for(var j=0;j<d[i].children.length;j++){
											if(d[i].children[j].isChoice==true){
                                              b+="<span><input name='res' type='checkbox' value='"+d[i].children[j].id+"' checked>"+d[i].children[j].name+"</span>"
											}else{
											  b+="<span><input name='res' type='checkbox' value='"+d[i].children[j].id+"'>"+d[i].children[j].name+"</span>"
											}
										  }
										  a+="<li><h5>"+d[i].name+"</h5>"+b+"</li>";
									  }else{
										  a+="<li><h5>"+d[i].name+"</h5></li>";
									  }
								  }
								  $("#modal-Permissions ul").html(a)
							  }else{
								$("#modal-Permissions ul").html("<li>无数据</li>")
							  }
							}else{
							   alert(data.msg)	
							}
						}
				   });
				}else{
				  $("#modal-Permissions .modal-body").attr("data-uid",uid)
				  $.ajax({
						url:'http://web.icecn.net/base/user/getUserResLimit.json',
						type:'POST',
						async:true,
						data:{
							userId:uid
						},
						dataType: "json",
					    xhrFields:{withCredentials:true},
						success: function(data){
							if(data.code === 200 ){
							  var a="";
							  var d=data.t;
							  if(d.length>0){
								  for(var i=0;i<d.length;i++){
									  if(d[i].children.length>0){
										  var b="";
										  for(var j=0;j<d[i].children.length;j++){
											if(d[i].children[j].isChoice==true){
                                              b+="<span><input name='res' type='checkbox' value='"+d[i].children[j].id+"' checked>"+d[i].children[j].name+"</span>"
											}else{
											  b+="<span><input name='res' type='checkbox' value='"+d[i].children[j].id+"'>"+d[i].children[j].name+"</span>"
											}
										  }
										  a+="<li><h5>"+d[i].name+"</h5>"+b+"</li>";
									  }else{
										  a+="<li><h5>"+d[i].name+"</h5></li>";
									  }
								  }
								  $("#modal-Permissions ul").html(a)
							  }else{
								$("#modal-Permissions ul").html("<li>无数据</li>")
							  }
							}else{
							   alert(data.msg)	
							}
						}
				   });
				}
			})
			//保存权限
			$(".btnPermissions").click(function(){
				var rid=$("#modal-Permissions .modal-body").attr("data-rid")
				var uid=$("#modal-Permissions .modal-body").attr("data-uid")
				if(uid==""){
					res="";
				   $("input[name=res]").each(function() {
						if ($(this).prop("checked")) {
							res += ","+$(this).val();
						}
				   }); 
				   res=res.substr(1)
					$.ajax({
						url:'http://web.icecn.net/base/role/saveRoleRes.json',
						type:'POST',
						async:true,
						data:{
							roleId:rid,
							res:res
						},
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
							   alert(data.msg)	
								$("#modal-Permissions").modal('hide');
								getlist(curpage,pagenum)
							}else{
							   alert(data.msg)	
							}
						}
				   });
			    }else{
					res="";
				   $("input[name=res]").each(function() {
						if ($(this).prop("checked")) {
							res += ","+$(this).val();
						}
				   }); 
				   res=res.substr(1)
					$.ajax({
						url:'http://web.icecn.net/base/user/saveUserRes.json',
						type:'POST',
						async:true,
						data:{
							userId:uid,
							res:res
						},
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
							   alert(data.msg)	
								$("#modal-Permissions").modal('hide');
								getlist(curpage,pagenum)
							}else{
							   alert(data.msg)	
							}
						}
				   });
				}
			})
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum)
			getlist2(curpage,pagenum)
		}
	};
	Page.init();
})();